<template>
  <a-col class="patrol" :span="theSpan" :md="theMd" :sm="theSm" :xs="theXs" :lg="theLg" :xl="theXl" :xxl="theXxl">
    <a-form-model-item :label="label" :prop="prop" :labelCol="theLabelCol" :wrapperCol="theWrapperCol">
      <div class="patrol-task" :class="{'patrol-task-default': size === 'default', 'patrol-task-large': size === 'large'}">
        <template v-for="(item, index) in inspectSetDataChild">
          <div class="patrol-task-item" :key="index">
            <div class="patrol-task-item-left">
              <a-form-model>
                <a-row :gutter="8" class="patrol-task-item-row">
                  <div class="patrol-task-item-label">督察点位</div>
                  <div class="patrol-task-item-content">{{item.g0202}}</div>
                  <div class="patrol-task-item-content" v-if="item.g0204Name">{{item.g0204Name}}</div>
                  <div class="patrol-task-item-content" v-if="item.g0205Name">{{item.g0205Name}}</div>
                  <div class="patrol-task-item-content" v-if="item.g0206Name">{{item.g0206Name}}</div>
                </a-row>
                <a-row :gutter="8" class="patrol-task-item-row">
                  <div class="patrol-task-item-label">抽检设置：</div>
                  <div class="patrol-task-item-content patrol-task-item-xcpl">抽检比例：{{item.g0208}} % ，随机抽检数量：{{item.g0209}} 处 ，随机督察人数：{{item.g0210}} 人</div>
<!--                  <div class="patrol-task-item-label">随机抽检数量：</div>
                  <div class="patrol-task-item-content patrol-task-item-xcpl">{{item.g0209}} 处</div>
                  <div class="patrol-task-item-label">随机督察人数：</div>
                  <div class="patrol-task-item-content patrol-task-item-xcpl">{{item.g0210}} 人</div>-->
                  <div class="patrol-task-item-operate" >
                    <a-icon type="form" v-show="buttonShow" @click="editInspectSet(index,item)"/>
                    <a-icon type="delete" v-show="buttonShow" @click="delInspectSet(index,item)"/>
                  </div>
                </a-row>
<!--                <a-row :gutter="8" class="patrol-task-item-row" v-if="formInfo.g0502 === '2'">
                  <div class="special-name">
                    {{ item.g0207 }}
                  </div>
                </a-row>-->
              </a-form-model>
            </div>
            <div class="patrol-task-item-right">
              <div class="patrol-task-item-count">
                <div class="patrol-task-item-count-bg" @click="showTasksList(index,item)">{{ item.pointCount }}
                  <span class="patrol-task-item-count-sub" >处</span></div>
              </div>
            </div>
          </div>
        </template>
        <div class="patrol-task-add">
          <a-button v-if="buttonShow" type="primary" block icon="plus" @click="addInspectSetData">
            {{this.inspectSetTitleChild}}
          </a-button>
        </div>
      </div>
    </a-form-model-item>
    <a-modal
        title="督察设置"
        centered
        style="top: 20px"
        width="55%"
        height="100%"
        destroyOnClose
        v-model="visible"
        cancelText="关闭"
        @ok="handleOk"
        @cancel="handleCancel"
    >
      <inspect-set-details-form
          ref="inspectSetDetailsForm"
          @handleCancel="handleCancel"
          @showOrHide="showOrHide"
          :set-inspect-data="inspectDataChange">
      </inspect-set-details-form>
      <template slot="footer">
        <ep-button @click="handleOk" >确认</ep-button>
        <ep-button @click="handleCancel">取消</ep-button>
      </template>
    </a-modal>

    <a-modal
        title="任务清单"
        centered
        style="top: 20px"
        width="75%"
        height="95%"
        destroyOnClose
        v-model="visible2"
        cancelText="关闭"
        @ok="handleOk"
        @cancel="handleCancel"
    >
      <inspect-point-list-form
          ref="inspectPointListForm"
          @handleCancel="handleCancel2"
          @showOrHide="showOrHide2"
          :resources-change="inspectDataChange"
      >
      </inspect-point-list-form>

      <template slot="footer">
        <a></a>
      </template>
    </a-modal>
  </a-col>
</template>

<script>
import {mapGetters} from "vuex";
import _ from 'lodash'

import modal from 'ant-design-vue/es/modal'
import inspectPointListForm from "@/pages/system/form/InspectPointListForm"
import {getAllPointCount} from "@/services/system/whdngxlApi";
import InspectSetDetailsForm from "@/pages/system/form/InspectSetDetailsForm";
export default {
  components:{
    InspectSetDetailsForm,
    inspectPointListForm,
  },
  name: "inspectTaskSetting",
  props: {
    // 类型 {normal: '常规', special: '专项'}
    type: {
      type: String,
      default: 'normal'
    },
    label: String,
    //label 标签布局，同 <Col> 组件，设置 span offset 值，如 {span: 3, offset: 12} 或 sm: {span: 3, offset: 12} 或者 固定宽度 :labelCol="{ style: 'width: 100px' }"
    labelCol: {
      type: Object,
      default: function () {
        return null
      }
    },
    //需要为输入控件设置布局样式时，使用该属性，用法同 labelCol 或者固定宽度搭配 :wrapperCol="{ style: {width: 'calc(100% - 100px)',display: 'inline-block'} }"
    wrapperCol: {
      type: Object,
      default: function () {
        return null
      }
    },
    prop: {
      type: String,
      default: ""
    },
    span: Number,
    formInfo: {
      required: false,
    },
    // 传入数据
    data: {
      type: Array,
      default: () => []
    }
  },
  computed: {
    ...mapGetters(['size']),
  },
  watch: {
    data: {
      handler(newVal) {
        this.list = newVal
      },
      deep: true,
    }

  },
  data() {
    return {
      theLabelCol: this.labelCol ? this.labelCol : this.$parent.$options.parent.labelCol,
      theWrapperCol: this.wrapperCol ? this.wrapperCol : this.$parent.$options.parent.wrapperCol,
      theSpan: this.getSpan(),
      theMd: this.getMd(),
      theSm: this.getSm(),
      theXs: this.getXs(),
      theLg: this.getLg(),
      theXl: this.getXl(),
      theXxl: this.getXxl(),
      visible: false,
      visible2: false,
      editIndex: "",
      settingType:"",
      inspectSetDataChild: [],
      inspectSetDataDelChild:[],
      allPointCountChild:null, //总点位数量
      choosePointCountChild:null, //已选点位数量
      surplusPointCountChild:null, //剩余点位数量
      inspectSetTitleChild:'添加',
      // alreadyInspectPoint:[],//已经设置的督察表单
      buttonShow:true, //编辑相关按钮是否显示
      g0501:""
    }
  },
  created() {

  },
  methods: {
    getSpan(data) {
      if (this.span) {
        this.mySpan = this.span
        return this.span
      }
      let span = null;
      let parent = null;
      if (data) {
        parent = data.$parent;
      } else {
        parent = this.$parent;
      }
      if (parent) {
        span = parent.span

        if (span === null || span === undefined) {
          let name = parent.$options.name
          if (name === 'ZhForm') {
            this.mySpan = undefined
            return undefined
          }
          return this.getSpan(parent)
        } else {
          this.mySpan = span
          return span
        }

      } else {
        this.mySpan = undefined
        return undefined
      }
    },
    getMd(data) {
      if (this.mySpan) {
        return undefined
      } else {
        this.getSpan()
      }
      if (this.md) {
        return this.md
      }
      let md = null;
      let parent = null;
      if (data) {
        parent = data.$parent;
      } else {
        parent = this.$parent;
      }
      if (parent) {
        md = parent.md
        if (md === null || md === undefined) {
          return this.getMd(parent)
        } else {
          return md
        }

      } else {
        return undefined
      }
    },
    getSm(data) {
      if (this.mySpan) {
        return undefined
      } else {
        this.getSpan()
      }
      if (this.sm) {
        return this.sm
      }
      let sm = null;
      let parent = null;
      if (data) {
        parent = data.$parent;
      } else {
        parent = this.$parent;
      }
      if (parent) {
        sm = parent.sm
        if (sm === null || sm === undefined) {
          return this.getSm(parent)
        } else {
          return sm
        }

      } else {
        return undefined
      }
    },
    getXs(data) {
      if (this.mySpan) {
        return undefined
      } else {
        this.getSpan()
      }
      if (this.xs) {
        return this.xs
      }
      let xs = null;
      let parent = null;
      if (data) {
        parent = data.$parent;
      } else {
        parent = this.$parent;
      }
      if (parent) {
        xs = parent.xs
        if (xs === null || xs === undefined) {
          return this.getXs(parent)
        } else {
          return xs
        }

      } else {
        return undefined
      }
    },
    getLg(data) {
      if (this.mySpan) {
        return undefined
      } else {
        this.getSpan()
      }
      if (this.lg) {
        return this.lg
      }
      let lg = null;
      let parent = null;
      if (data) {
        parent = data.$parent;
      } else {
        parent = this.$parent;
      }
      if (parent) {
        lg = parent.lg
        if (lg === null || lg === undefined) {
          return this.getLg(parent)
        } else {
          return lg
        }

      } else {
        return undefined
      }
    },
    getXl(data) {
      if (this.mySpan) {
        return undefined
      } else {
        this.getSpan()
      }
      if (this.xl) {
        return this.xl
      }
      let xl = null;
      let parent = null;
      if (data) {
        parent = data.$parent;
      } else {
        parent = this.$parent;
      }
      if (parent) {
        xl = parent.xl
        if (xl === null || xl === undefined) {
          return this.getXl(parent)
        } else {
          return xl
        }

      } else {
        return undefined
      }
    },
    getXxl(data) {
      if (this.mySpan) {
        return undefined
      } else {
        this.getSpan()
      }
      if (this.xxl) {
        return this.xxl
      }
      let xxl = null;
      let parent = null;
      if (data) {
        parent = data.$parent;
      } else {
        parent = this.$parent;
      }
      if (parent) {
        xxl = parent.xxl
        if (xxl === null || xxl === undefined) {
          return this.getXxl(parent)
        } else {
          return xxl
        }

      } else {
        return undefined
      }
    },
    initData(data){
      if(data.g0501){
        this.buttonShow = false
        this.g0501 = data.g0501
      }
      this.inspectSetDataChild = data.inspectSetData
      this.inspectSetDataDelChild = data.inspectSetDataDel
      this.allPointCountChild = data.allPointCount
      this.choosePointCountChild = data.choosePointCount
      // this.surplusPointCountChild = data.surplusPointCount
      // this.inspectSetTitleChild = data.inspectSetTitle
    },
    inspectDataChange(data){
      if(data.flag === "edit"){
        this.settingType = "编辑"
      }

      if(this.settingType === "编辑"){
        this.inspectSetDataChild.splice(this.editIndex, 1, data);
      }else{
        this.inspectSetDataChild.push(data)
      }
      this.choosePointCountChild  =this.choosePointCountChild-data.beforeChooseCount+ data.pointCount
    /*  this.surplusPointCountChild = this.allPointCountChild-this.choosePointCountChild
      this.inspectSetTitleChild = '还有'+this.surplusPointCountChild+'处文保单位未分配任务，继续分配'*/

      const inspectDataChange = {}
      inspectDataChange.inspectSetDataChild = this.inspectSetDataChild
      inspectDataChange.choosePointCountChild = this.choosePointCountChild
    /*  inspectDataChange.surplusPointCountChild = this.surplusPointCountChild
      inspectDataChange.inspectSetTitleChild = this.inspectSetTitleChild*/
      inspectDataChange.inspectSetDataDelChild = this.inspectSetDataDelChild
      this.$emit("inspectDataChange", inspectDataChange);
    },
    //显示任务清单
    showTasksList(index,item){
      this.visible2 = true;
      this.settingType = "编辑"
      this.editIndex = index;
      item.g0502 = this.formInfo.g0502
      item.c0303 = this.formInfo.c0303
      item.g0503 = this.formInfo.g0503
      item.g0504 = this.formInfo.g0504
      if(this.g0501 != '' || this.g0501 !=null){
        item.g0501 = this.g0501
      }
      this.$nextTick(() => {
        this.$refs.inspectPointListForm.initData(JSON.parse(JSON.stringify(item)));
      })
    },
    // 把数据增加到list中
    addInspectSetData(){
      if(!this.formInfo.c0303){
        modal.warn({title: '提示', content: '请先选择资源表单'})
        return
      }
      if(!this.formInfo.g0503){
        modal.warn({title: '提示', content: '请先选择开始时间'})
        return
      }
      if(!this.formInfo.g0504){
        modal.warn({title: '提示', content: '请先选择结束间'})
        return
      }
      this.settingType="新增"
      this.showOrHide();
      this.$nextTick(() => {
        const data = {}
        data.g0502 = this.formInfo.g0502
        data.c0303 = this.formInfo.c0303
        data.g0503 = this.formInfo.g0503
        data.g0504 = this.formInfo.g0504
      /*  this.alreadyInspectPointChild = this.formInfo.alreadyInspectPoint
        data.alreadyInspectPoint = this.alreadyInspectPointChild*/
        this.$refs.inspectSetDetailsForm.initData(data);
      });
    },
    editInspectSet(index,item){
      if(!this.formInfo.c0303){
        modal.warn({title: '提示', content: '请先选择资源表单'})
        return
      }
      if(!this.formInfo.g0503){
        modal.warn({title: '提示', content: '请先选择开始时间'})
        return
      }
      if(!this.formInfo.g0504){
        modal.warn({title: '提示', content: '请先选择结束间'})
        return
      }
      this.settingType="编辑"
      this.visible = true;
      this.editIndex = index;
      this.$nextTick(() => {
        if (item.g0201) {
          item.flag = "edit";
        }
        item.g0502 = this.formInfo.g0502
        item.c0303 = this.formInfo.c0303
        item.g0503 = this.formInfo.g0503
        item.g0504 = this.formInfo.g0504
     /*   item.alreadyInspectPoint = this.alreadyInspectPointChild
        item.editIndex = index*/
        this.$refs.inspectSetDetailsForm.initData(JSON.parse(JSON.stringify(item)));
      });
    },
    delInspectSet(index,item){
      modal.confirm({
        title: "确认删除吗？",
        onOk: () => {
          if (this.inspectSetDataChild[index].g0201) {
            const item = JSON.parse(JSON.stringify(this.inspectSetDataChild[index]));
            item.flag = "delete";
            this.inspectSetDataDelChild.push(item);
          }
          this.inspectSetDataChild.splice(index, 1);
          // this.alreadyInspectPointChild.splice(index, 1);
         /* if(item.pointCount){
            this.surplusPointCountChild = this.surplusPointCountChild+item.pointCount
            this.inspectSetTitleChild = '还有'+this.surplusPointCountChild+'处文保单位未分配任务，继续分配'
          }
          if(this.inspectSetDataChild.length === 0){
            this.inspectSetTitleChild = '添加'
          }*/
          const inspectDataChange = {}
          inspectDataChange.inspectSetDataChild = this.inspectSetDataChild
          inspectDataChange.choosePointCountChild = this.choosePointCountChild
          // inspectDataChange.surplusPointCountChild = this.surplusPointCountChild
          // inspectDataChange.inspectSetTitleChild = this.inspectSetTitleChild
          inspectDataChange.inspectSetDataDelChild = this.inspectSetDataDelChild
          // inspectDataChange.alreadyInspectPoint = this.alreadyInspectPoint
          this.$emit("inspectDataChange", inspectDataChange);
        },
        onCancel: () => {},
      })
    },
    c0303Change(data){
      this.inspectSetDataChild = []
      this.inspectSetDataDelChild = []
      this.inspectSetTitleChild = '添加'
    },
    // 关闭弹窗
    handleCancel() {
      this.visible = false;
    },
    showOrHide() {
      this.visible = !this.visible;
    },
    handleCancel2() {
      this.visible2 = false;
    },
    showOrHide2() {
      this.visible2 = !this.visible2;
    },
    handleOk() {
      this.$nextTick(() => {
        this.$refs.inspectSetDetailsForm.confirm();
      });
    },
  }
}
</script>

<style scoped lang="less">
.patrol-task {

  &.patrol-task-large {
    .patrol-task-item-label {
      height: 40px;
      line-height: 40px;
    }
    .patrol-task-item-content {
      height: 40px;
      line-height: 40px;
    }
    .patrol-task-item-operate {
      height: 40px;
      line-height: 40px;
    }
  }

  &.patrol-task-large-default {
    .patrol-task-item-label {
      height: 32px;
      line-height: 32px;
    }
    .patrol-task-item-content {
      height: 32px;
      line-height: 32px;
      font-size: 12px;
    }
    .patrol-task-item-operate {
      height: 32px;
      line-height: 32px;
    }
  }

  .patrol-task-item {
    display: flex;
    margin-bottom: 15px;
    padding: 10px 10px;
    border: 1px solid #3e9fc8;
    border-radius: 5px;
    background: #eceff8;

    .patrol-task-item-left {
      flex: auto;

      .patrol-task-item-row {
        display: flex;
        flex-wrap: wrap;
        &:not(:last-child) {
          margin-bottom: 10px;
        }

        .special-name {
          min-width: 300px;
          height: 32px;
          line-height: 32px;
          background: #d3dfef;
          text-align: center;
          color: #156abc;
          &:hover {
            background: #c1d6ec;
          }
        }
      }
      .patrol-task-item-label {
        width: 70px;
        color: #333333;
      }

      .patrol-task-item-content {
        margin-right: 10px;
        margin-bottom: 10px;
        padding: 0 10px;
        border-radius: 4px;
        background: #d3dfef;
        color: #5d5e5f;

        &.patrol-task-item-xcpl {
          min-width: 250px;
        }
      }

      .patrol-task-item-operate {
        color: #3e9fc8;

        .anticon {
          margin-right: 10px;
        }
      }
    }
    .patrol-task-item-right {
      width: 100px;
      margin-left: 10px;
      padding: 0px;
      .patrol-task-item-count {
        width: 100px;
        height: 100px;
        padding: 12px;
        border-radius: 50%;
        background: #FFFFFF;
        box-shadow: 0 0 10px 1px rgba(26, 64, 79, 0.2);

        .patrol-task-item-count-bg {
          height: 100%;
          line-height: 76px;
          border-radius: 50%;
          border: 2px solid #d9e7f5;
          text-align: center;
          color: #156abc;
          font-size: 18px;
          .patrol-task-item-count-sub {
            font-size: 14px;
          }
        }
      }
    }
  }
}

.frequency {
  display: flex;
  .frequency-box {
    flex: auto;
    .frequency-item {
      margin-right: 10px;
      vertical-align: middle;
    }
  }
}

.special-form {
  .special-form-index {
    display: inline-block;
    width: 40px;

  }
  .special-form-add {
    width: 50%;
    padding-left: 40px;
    .ant-btn {
      border: transparent;
      background: #d3dfef;
      color: #156abc;
      &:hover {
        background: #c1d6ec;
      }
    }
  }

  .special-form-item-type {
    margin-left: 15px;
  }
  .special-form-item-required {
    margin-left: 15px;
  }
  .special-form-item-delete {
    margin-left: 15px;
    color: #3e9fc8;
    font-size: 18px;
  }

  .special-form-subitem {
    padding-left: 40px;
    .special-form-subitem-index {
      display: inline-block;
      width: 50px;
    }
    .special-form-subitem-add {
      display: inline-block;
      padding: 5px 10px;
      cursor: pointer;
      color: #3e9fc8;
    }
  }
}

</style>
